using System;
using System.Data;
using System.Data.Common;
using System.Text;

namespace Adoor.Relational.RelationalSource
{
	/// <summary>
	/// Summary description for RelationalSourceTrace.
	/// </summary>
	public class RelationalSourceTrace
	{
		public RelationalSourceTrace()
		{
			
		}

		public virtual string GetTrace(string commandText, System.Data.IDbCommand command)
		{
			StringBuilder trace = new StringBuilder(commandText);
			if (command.Parameters.Count != 0)
			{
				trace.Append(" (");
				bool comma = false;
				foreach(IDbDataParameter prm in command.Parameters)
				{
					if (comma)
						trace.Append(", ");
					comma = true;

					trace.Append(prm.ParameterName);
					trace.Append(" = '");
					trace.Append(Convert.ToString(prm.Value, System.Globalization.CultureInfo.InvariantCulture));
					trace.Append("'");
				}
				trace.Append(")");
			}
			return trace.ToString();
		}

		public virtual string GetDBTypeString(IDbDataParameter prm)
		{
			switch(prm.DbType)
			{
				case DbType.String:
				case DbType.StringFixedLength:
				case DbType.AnsiString:
				case DbType.AnsiStringFixedLength:
					return "varchar(" + (Convert.ToString(prm.Value).Length + 1) + ")";					
				case DbType.Binary:
					return "longblob";
				case DbType.Boolean:
					return "bit";
				case DbType.Byte:
					return "byte";
				case DbType.Currency:
					return "currency";
				case DbType.Date:
				case DbType.DateTime:
				case DbType.Time:
					return "datetime";
				case DbType.Decimal:
					return "decimal(15,4)";
				case DbType.Double:
					return "double(15,4)";
				case DbType.Guid:
					return "uniqueidentifier";
				case DbType.Int16:
					return "int";
				case DbType.Int32:
					return "int";
				case DbType.Int64:
					return "int";
				case DbType.Single:
					return "float(15,4)";
				default:
					return prm.DbType.ToString();
			}
		}
	}
}